package cn.codeforfun.base;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import java.util.Date;
import java.util.UUID;

/**
 * @author wangbin
 */
@Getter
@Setter
@MappedSuperclass
@JsonIgnoreProperties({"hibernateLazyInitializer"})
public abstract class BaseEntity {
    @Id
    @Column(length = 36)
    protected String id;
    protected Date createTime;
    protected Date updateTime;
    protected Boolean deleted;

    public String getId() {
        if (id == null) {
            this.id = UUID.randomUUID().toString();
        }
        return id;
    }

    public Date getCreateTime() {
        if (createTime == null) {
            this.createTime = new Date();
        }
        return createTime;
    }

    public Date getUpdateTime() {
        if (updateTime == null) {
            this.updateTime = new Date();
        }
        return updateTime;
    }

    public Boolean getDeleted() {
        if (deleted == null) {
            this.deleted = false;
        }
        return deleted;
    }
}
